<?php

class Default_SoapController extends Zend_Controller_Action
{

    public function preDispatch ()
    {
        $this->domain = 'http://' . $_SERVER['SERVER_NAME'];
    }

    public function soapAction ()
    {
        $data = file_get_contents('php://input');
//         file_put_contents(APPLICATION_PATH . '/../data/input.xml', $data);
        // initialize server and set URI
        $server = new Zend_Soap_Server(null, 
                array(
                        'uri' => $this->domain . '/default/soap/soap'
                ));
        
        // set SOAP service class
        $server->setClass('Default_Service_Soap');
        
        // register exceptions that generate SOAP faults
        $server->registerFaultException(
                array(
                        'Exception'
                ));
        // handle request
        $server->handle($data);
    }

    public function wsdlAction ()
    {
        // set up WSDL auto-discovery
        $wsdl = new Zend_Soap_AutoDiscover();
        
        // attach SOAP service class
        $wsdl->setClass('Default_Service_Soap');
        
        $apiUrl = $this->domain . '/default/soap/soap';
        // set SOAP action URI
        $wsdl->setUri($apiUrl);
        
        // handle request
        $wsdl->handle();
    }

    public function indexAction ()
    {
       
        // initialize SOAP client
        $options = array(
                'location' => $this->domain . '/default/soap/soap',
                'uri' => $this->domain . '/default/soap/soap'
        );
        
        try
        {
            $client = new Zend_Soap_Client(null, $options);
            $req = array(
                    'AsnCode' => 'RA0256120530010',
                    "AsnStatus" => 1,
                    'HeaderRequest' => array(
                            'customerId' => 'A0256',
                            'appToken' => 'asdffffffffffffffffffffffffasdfasdfasdfasdfasdfaffffffffffffffffffffffffffffffddddddddddddddddd',
                            'appKey' => 'ffdddfdfdeaadffffffffffffffasdf'
                    )
            );
            
            $result = $client->testProduct($req);
            print_r($result);
        } catch (SoapFault $s)
        {
            die('ERROR: [' . $s->faultcode . '] ' . $s->faultstring);
        } catch (Exception $e)
        {
            die('ERROR: ' . $e->getMessage());
        }
    }

    public function testAction ()
    {
        
        // initialize SOAP client
        $options = array(
                'location' => $this->domain . '/default/soap/soap',
                'uri' => $this->domain . '/default/soap/soap'
        );
        try
        {
            $client = new Zend_Soap_Client(null, $options);
            $req = array(
                    'AsnCode' => 'RA0256120530010',
                    "AsnStatus" => 1,
                    'HeaderRequest' => array(
                            'customerId' => 'A0256',
                            'appToken' => 'asdffffffffffffffffffffffffasdfasdfasdfasdfasdfaffffffffffffffffffffffffffffffddddddddddddddddd',
                            'appKey' => 'ffdddfdfdeaadffffffffffffffasdf'
                    )
            );
            $result = $client->testProduct($req);
            print_r($result); // exit;
            $data = file_get_contents('php://output');
            file_put_contents(APPLICATION_PATH . '/../data/output.xml', $client->getLastRequest());
            
            file_put_contents(APPLICATION_PATH . '/../data/output.xml', 
                    $client->getLastResponse());
            
            var_dump($client->getLastRequest());
            var_dump($client->getLastResponse());
            
        } catch (SoapFault $s)
        {
            die('ERROR: [' . $s->faultcode . '] ' . $s->faultstring);
        } catch (Exception $e)
        {
            die('ERROR: ' . $e->getMessage());
        }
    }

    public function test1Action ()
    {
        try
        {
            $client = new Zend_Soap_Client($this->domain . '/default/soap/wsdl');
            $req = array(
                    'AsnCode' => 'RA0256120530010',
                    "AsnStatus" => 1,
                    'HeaderRequest' => array(
                            'customerId' => 'A0256',
                            'appToken' => 'asdffffffffffffffffffffffffasdfasdfasdfasdfasdfaffffffffffffffffffffffffffffffddddddddddddddddd',
                            'appKey' => 'ffdddfdfdeaadffffffffffffffasdf'
                    )
            );
            $result = $client->testProduct($req);
            print_r($result); // exit;
            
            file_put_contents(APPLICATION_PATH . '/../data/output1.xml', 
                    $client->getLastResponse());

            file_put_contents(APPLICATION_PATH . '/../data/input1.xml',
            $client->getLastRequest());
        } catch (SoapFault $s)
        {
            die('ERROR: [' . $s->faultcode . '] ' . $s->faultstring);
        } catch (Exception $e)
        {
            die('ERROR: ' . $e->getMessage());
        }
    }

    public function nusoapAction ()
    {
        $req = array(
                'AsnCode' => 'RA0256120530010',
                "AsnStatus" => 1,
                'HeaderRequest' => array(
                        'customerId' => 'A0256',
                        'appToken' => 'asdffffffffffffffffffffffffasdfasdfasdfasdfasdfaffffffffffffffffffffffffffffffddddddddddddddddd',
                        'appKey' => 'ffdddfdfdeaadffffffffffffffasdf'
                )
        );
        try
        {
        	nusoap_server();
            require_once 'nusoap/nusoap.php';
            // echo $this->domain . '/default/soap/wsdl';exit;
            // echo (int)class_exists('nusoap_client');exit;
            // 之后还要使用第二个参数以便使client知道我们传递过来的是WSDL，而不是SOAP endpoint。
            $s = new nusoap_client($this->domain . '/default/soap/wsdl', true);
            // 生成proxy类
            $p = $s->getProxy();
            // 调用远程函数
            
            $sq = $p->testProduct($req);
            
            $data = $p->response;
            var_dump($data);
            file_put_contents(APPLICATION_PATH . '/../data/nusoap.xml', $data);
        } catch (SoapFault $s)
        {
            die('ERROR: [' . $s->faultcode . '] ' . $s->faultstring);
        } catch (Exception $e)
        {
            die('ERROR: ' . $e->getMessage());
        }
    }
    
    // 图片上传
    public function uploadAction ()
    {
        // initialize SOAP client
        $options = array(
                'location' => $this->domain . '/default/soap/soap',
                'uri' => $this->domain . '/default/soap/soap'
        );
        
        try
        {
            $client = new Zend_Soap_Client(null, $options);
            $req = array(
                    'HeaderRequest' => array(
                            'customerId' => 'A0256',
                            'appToken' => 'asdffffffffffffffffffffffffasdfasdfasdfasdfasdfaffffffffffffffffffffffffffffffddddddddddddddddd',
                            'appKey' => 'ffdddfdfdeaadffffffffffffffasdf'
                    ),
                    "pictures" => array(
                            array(
                                    "fileName" => "logo.png",
                                    "data" => base64_encode(
                                            file_get_contents(
                                                    APPLICATION_PATH .
                                                             "/../public/images/left-menu-title.png"))
                            )
                    )
            );
            
            $result = $client->uploadProduct($req);
            var_dump($client);
            print_r($result);
        } catch (SoapFault $s)
        {
            die('ERROR: [' . $s->faultcode . '] ' . $s->faultstring);
        } catch (Exception $e)
        {
            die('ERROR: ' . $e->getMessage());
        }
    }
}